package net.csdn.business.mirror.pop.controller;


import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.annotation.Inner;
import net.csdn.business.common.enums.ResultCodeEnum;
import net.csdn.business.common.exception.BusinessException;
import net.csdn.business.mirror.SpringBeanUtil;
import net.csdn.business.mirror.pop.bo.UserBO;
import net.csdn.business.mirror.pop.dto.GitAuthorizeDto;
import net.csdn.business.mirror.pop.dto.GitOrgDto;
import net.csdn.business.mirror.pop.dto.GitRepoDto;
import net.csdn.business.mirror.pop.dto.MyGithubDto;
import net.csdn.business.mirror.pop.enums.UserTokenEnum;
import net.csdn.business.mirror.pop.vo.UserTokenVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@Inner
@RestController()
@RequestMapping("/api/v1/mirror/thirdApi")
public class ThirdGitApiController {

    /**
     * 分页获取用户授权的组织信息
     */
    @PostMapping("/getOrgs")
    public MyGithubDto getOrgs(@RequestBody GitOrgDto gitOrgDto) {
        MyGithubDto myGithub = new MyGithubDto();
        myGithub.setToken(gitOrgDto.getAccess_token());
        UserTokenEnum.Type t = UserTokenEnum.Type.parse(gitOrgDto.getPlatform());
        if (Objects.isNull(t)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "type");
        }
        SpringBeanUtil.getBean(t.getThirdApiService()).listUserAllOrgs(myGithub, gitOrgDto.getPage(), gitOrgDto.getPageSize());
        return myGithub;
    }

    /**
     * 分页获取用户授权的仓库信息
     */
    @PostMapping("/getRepos")
    public MyGithubDto getRepos(@RequestBody GitRepoDto gitRepoDto) {
        MyGithubDto myGithub = new MyGithubDto();
        myGithub.setToken(gitRepoDto.getAccess_token());
        UserTokenEnum.Type t = UserTokenEnum.Type.parse(gitRepoDto.getPlatform());
        if (Objects.isNull(t)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "type");
        }
        SpringBeanUtil.getBean(t.getThirdApiService()).listUserAllRepos(myGithub, gitRepoDto.getPage(), gitRepoDto.getPageSize());
        return myGithub;
    }


    /**
     * 根据token获取用户信息
     */
    @PostMapping("/getUserByToken")
    public UserBO getUserByToken(@RequestBody GitAuthorizeDto authorizeDto) {
        UserTokenEnum.Type t = UserTokenEnum.Type.parse(authorizeDto.getPlatform());
        if (Objects.isNull(t)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "type");
        }
        String token = authorizeDto.getAccess_token();
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "token");
        }
        return SpringBeanUtil.getBean(t.getThirdApiService()).getUser(token);
    }


    /**
     * 获取授权信息code并重定向
     */
    @GetMapping("/authorize/{platform}")
    public String authorize(@PathVariable("platform") String platform) {
        UserTokenEnum.Type t = UserTokenEnum.Type.parse(platform);
        if (Objects.isNull(t)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "type");
        }
        return SpringBeanUtil.getBean(t.getThirdApiService()).authorize();
    }


    /**
     * 根据code和具体哪个平台获取token信息
     */
    @PostMapping("/token")
    public UserTokenVo getToken(@Validated @RequestBody GitAuthorizeDto authorizeDto) {
        String code = authorizeDto.getCode();
        if (StringUtils.isBlank(code)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "code");
        }
        UserTokenEnum.Type t = UserTokenEnum.Type.parse(authorizeDto.getPlatform());
        if (Objects.isNull(t)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "type");
        }
        return SpringBeanUtil.getBean(t.getThirdApiService()).getToken(code);
    }
}